package com.lijing.mock;

import com.lijing.mock.constract.MongoTableConstant;
import com.mongodb.client.model.IndexOptions;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 系统初次启动时初始化mongodb表信息
 * @author by lijing
 * @date 2019/12/6 0006 10:31
 */
@Slf4j
@Component
public class InitMongodb {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 初始化 mongodb 数据库表
     */
    public void init(){
        if(!mongoTemplate.collectionExists(MongoTableConstant.BASE_URL_CONFIG)) {
            log.info("Mongodb Table {} not exist init it",MongoTableConstant.BASE_URL_CONFIG);
            mongoTemplate.createCollection(MongoTableConstant.BASE_URL_CONFIG);
            mongoTemplate.getCollection(MongoTableConstant.BASE_URL_CONFIG).createIndex(new Document().append("url",1).append("projectName",1), new IndexOptions().unique(true).name("url_project_unique"));
        }
        if(!mongoTemplate.collectionExists(MongoTableConstant.URL_MOCK_RESULT_DATA)) {
            log.info("Mongodb Table {} not exist init it", MongoTableConstant.URL_MOCK_RESULT_DATA);
            mongoTemplate.createCollection(MongoTableConstant.URL_MOCK_RESULT_DATA);
            mongoTemplate.getCollection(MongoTableConstant.URL_MOCK_RESULT_DATA).createIndex(new Document("urlId",1),new IndexOptions().name("urlId_index"));
        }
    }
}
